# example7_13
def make_averager():
    count = 0
    total = 0

    def averager(new_value):
        # count += 1
        # total += new_value
        # return total / count
        # 以上注释打开，会报错
        pass

    return averager

# https://www.python.org/dev/peps/pep-3104/
# 参考上面的链接，改写代码，在不使用`nonlocal`的情况下使用闭包

class Namespace:
    pass

def make_averager2():
    ns = Namespace()
    ns.count = 0
    ns.total = 0

    def averager(new_value):
        print('ns.count=%s,ns.total=%s' % (ns.count, ns.total))
        ns.count += 1
        ns.total += new_value
        return ns.total / ns.count

    return averager

if __name__ == '__main__':
    avg = make_averager2()
    print(avg(10))
    print(avg(11))
    print(avg(12))
    print(avg(13))
    print(avg(11))
    print(avg(11))
